﻿using System;

using System.Collections.Generic;
using System.IO;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Controls.Classes;
using Silvermoon.Animations;
using System.Drawing;
using System.Xml;
using MelectricComponents;


namespace FileExplorer
{
    public class AddWeightWindow : Window
    {
        private Toolbar toolbar;
        private BackgroundDecorator background;

        TextBox txtstone;
        TextBox txtDateTime;
        DropDownButton dropDownStones;
        DropDownButton dropDownPounds;
        DropDownButton dropDownTenths;

        public AddWeightWindow()
            : base()
        {
            this.Flags = ControlFlags.IgnoreChildClipping;
            background = new BackgroundDecorator { Color = System.Drawing.Color.Silver };
            background.Margin = new Margin(0);
            Controls.Add(background);
            txtDateTime = new TextBox
            {
                Alpha = 128
                ,
                ToolTip = new DatePickerToolTip()
                ,
                Size = new Size(Screen.Width, 100)
                ,
                Location = new Point(0, 0)
                ,
                Text = DateTime.Now.ToString("dd/MM/yyyy")
            };
            Controls.Add(txtDateTime);
            dropDownStones = new DropDownButton { Bounds = new Rectangle(0, 100, Screen.Size.Width / 8 * 3, 100), Text = "Stones", Color = Color.Snow, TextColor = Color.Black };
            dropDownStones.AddItems("13", "12", "11", "Other");
            Controls.Add(dropDownStones);
            dropDownStones.ItemClicked += new EventHandler<ItemEventArgs>(OnStonesItemClicked);
            txtstone = new TextBox
                                    {
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8 * 3
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                        ,
                                        Location = new Point(0, 100)
                                        ,
                                        Visible=false
                                        ,
                                        Text="0"
                                    };
            Controls.Add(txtstone);
            Label lblstone = new Label
                                    {
                                        Text = "st"
                                        ,
                                        Alpha = 128
                                        ,
                                        Height = 100
                                        ,
                                        Width = Screen.Size.Width / 8
                                        ,
                                        Location = new Point(Screen.Size.Width / 8 * 3, 100)
                                        ,
                                        Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
                                    };
            Controls.Add(lblstone);
            dropDownPounds = new DropDownButton { Bounds = new Rectangle(Screen.Size.Width / 8 * 4, 100, Screen.Size.Width / 8 * 2, 100), Text = "0", Color = Color.Snow, TextColor = Color.Black };
            dropDownPounds.AddItems("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13");
            Controls.Add(dropDownPounds);

            Label lblpoint = new Label
            {
                Text = "."
                ,
                Alpha = 128
                ,
                Height = 100
                ,
                Width = Screen.Size.Width / 8
                ,
                Location = new Point(Screen.Size.Width / 8 * 6, 100)
                ,
                TextColor = SysColors.TextColor
                ,
                Font = new Silvermoon.OpenGL.FontSprite(new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))
            };
            Controls.Add(lblpoint);

            dropDownTenths = new DropDownButton { Bounds = new Rectangle(Screen.Size.Width / 16 * 13, 100, Screen.Size.Width / 16 * 3, 100), Text = "0", Color = Color.Snow, TextColor = Color.Black };
            dropDownTenths.AddItems("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
            Controls.Add(dropDownTenths);

            toolbar = new Toolbar();
            Toolbar = toolbar;
            toolbar.LeftButton.Text = "Cancel";
            toolbar.RightButton.Text = "Submit";
            toolbar.LeftButton.Click += (s, e) => { txtstone.Visible = false; dropDownStones.Visible = true; Hide(TransitionMode.ZoomOut); };
            toolbar.RightButton.Click += (s, e) => { SaveData(); };
        }

        private void SaveData()
        {
            Dictionary<string,string> Attributes = new Dictionary<string,string>();
            Attributes.Add("DateTime", txtDateTime.Text);
            Attributes.Add("Stones", txtstone.Text);
            Attributes.Add("Pounds", dropDownPounds.Text + "." + dropDownTenths.Text);
            XMLUtility oXMLUtility = new XMLUtility();
            XmlNode WeightNode = oXMLUtility.SaveXML("Weight", "Measurement", Attributes);

            XmlNode PreviousNode = WeightNode.ChildNodes[WeightNode.ChildNodes.Count - 2];
            double PreviousPounds = Convert.ToDouble(PreviousNode.Attributes["Stones"].Value) * 14 + Convert.ToDouble(PreviousNode.Attributes["Pounds"].Value);
            double CurrentPounds = Convert.ToDouble(txtstone.Text) * 14 + Convert.ToDouble(dropDownPounds.Text + "." + dropDownTenths.Text);
            double WeightLost = PreviousPounds - CurrentPounds;
            ConfirmDialog dlg = new ConfirmDialog("Weight Lost", WeightLost.ToString(), true);
            dlg.Margin = new Margin(0, null, 0, 0);
            dlg.HideTransition = TransitionMode.SwipeOutToBottom;
            dlg.ShowModal(TransitionMode.SwipeInFromBottom, (s, e) =>
            {
                txtstone.Text = "0";
                dropDownStones.Text = "Stones";
                dropDownPounds.Text = "0";
                dropDownTenths.Text = "0";
                txtDateTime.Text = DateTime.Now.ToString("dd/MM/yyyy");
                Hide(TransitionMode.ZoomOut);
            });



        }
        protected virtual void OnStonesItemClicked(object sender, ItemEventArgs e)
        {
            // if (ItemClicked != null) dropDown.ItemClicked(this, e);
            MenuItem item = e.Item as MenuItem;
            if (item != null)
            {
                if (item.Text == "Other")
                {
                    dropDownStones.Visible = false;
                    txtstone.Text = "";
                    txtstone.Visible = true;
                }
                else
                {
                    txtstone.Text = item.Text;
                }
            }
        }
    }
}
